# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def rightSideView(self, root: TreeNode) -> list:
        self.res, search_list = [], [root]
        if root:
            self.search(search_list)
        # for i in range(len(self.res)):
        #     self.res[i] = self.res[i][-1]
        return self.res

    def search(self, list_in):
        search_list = []
        for i in range(len(list_in)):
            if list_in[i].left:
                search_list.append(list_in[i].left)
            if list_in[i].right:
                search_list.append(list_in[i].right)
        list_in = list_in[-1].val
        self.res.append(list_in)
        if search_list:
            self.search(search_list)
